package com.situ.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.bean.Staff;
import com.situ.mapper.StaffMapper;
import com.situ.service.StaffService;

@Service
public class StaffServiceImpl implements StaffService {

	@Autowired
	private StaffMapper staffMapper;

	@Override
	public Staff add(Staff staff) {
		staffMapper.insert(staff);
		return staffMapper.selectByPrimaryKey(staff.getId());
	}

	@Override
	public Staff remove(Integer id) {
		Staff staff = staffMapper.selectByPrimaryKey(id);
		staffMapper.deleteByPrimaryKey(id);
		return staff;
	}

	@Override
	public Staff edit(Staff staff) {
		staffMapper.updateByPrimaryKey(staff);
		return staffMapper.selectByPrimaryKey(staff.getId());
	}

	@Override
	public Staff getById(Integer id) {
		return staffMapper.selectByPrimaryKey(id);
	}

	@Override
	public List getAll() {
		return staffMapper.selectAll();
	}

	@Override
	public PageInfo getByPage(Integer page, Integer limit, Staff staff) {

		if (page == null)
			page = 1;
		if (limit == null) {
			// 获取当前所有记录的条数
			List staffs = staffMapper.select(staff);
			limit = staffs.size();
		}
		// 1开启分页
		PageHelper.startPage(page, limit);
		// 调用
		List staffs = staffMapper.select(staff);
		// 生成PafgeInfo
		PageInfo pageInfo = new PageInfo<>(staffs);

		return pageInfo;
	}

}
